package servlets;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import webservices.helper.Helper;
import control.ControlService;

/**
 * Servlet implementation class CourseManageServlet
 */
public class HomeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request,response);
	}
	
	private void processRequest(HttpServletRequest request, HttpServletResponse response){
		try{
			request.setCharacterEncoding("UTF-8");
		} catch (UnsupportedEncodingException e){}
		
		String userID = (String)request.getSession().getAttribute("userID");
		String requestXML = "<param name='userID' value='" + (userID == null ? 1 : userID) + "' />";
		
		String responseXML = new ControlService().Call("Home", requestXML);
		
		try {
			NodeList responseList = Helper.getResponse(responseXML);
			
			String favJson = "[";
			String downJson = "[";
			
			for (int i = 0; i < responseList.getLength(); i++) {
				Element r = (Element)responseList.item(i);
				String name = r.getAttribute("name");

				if ("changelog".equals(name)){
					favJson += "{courseID: '" + r.getElementsByTagName("courseID").item(0).getTextContent() + "',";
					favJson += "courseName: '" + r.getElementsByTagName("courseName").item(0).getTextContent() + "',";
					favJson += "changeTime: '" + r.getElementsByTagName("changeTime").item(0).getTextContent() + "',";
					favJson += "log: '" + r.getElementsByTagName("log").item(0).getTextContent() + "'},";
				} else if ("downloadHistory".equals(name)){
					downJson += "{courseID: '" + r.getElementsByTagName("courseID").item(0).getTextContent() + "',";
					downJson += "courseName: '" + r.getElementsByTagName("courseName").item(0).getTextContent() + "',";
					downJson += "slideName: '" + r.getElementsByTagName("slideName").item(0).getTextContent() + "',";
					downJson += "downloadTime: '" + r.getElementsByTagName("downloadTime").item(0).getTextContent() + "'},";
				}
			}
			favJson += "]";
			downJson += "]";
			response.setCharacterEncoding("utf-8");
			response.getWriter().write("{changelog:"+favJson + ", downloadHistory:" + downJson + "}");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
